package com.qf.jie.controller;

import com.qf.jie.controller.servlet.BaseServlet;
import com.qf.jie.entity.po.User;
import com.qf.jie.entity.vo.RespBean;
import com.qf.jie.service.UserService;
import com.qf.jie.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/UserController")
public class UserController extends BaseServlet {

    UserService userService = new UserServiceImpl();
    // 1.登陆
    protected RespBean login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        return userService.login(req, username, password);
    }
    // 2.注册
    protected RespBean register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String strUser_type = req.getParameter("user_type");
        String sex = req.getParameter("sex");
        String phone = req.getParameter("phone");
        // 处理前端不传参数或参数不是数字，传0则为0，传其它或者不传则为1
        int user_type = 1;
        if (strUser_type == null || strUser_type.equals("0")){
            user_type = 0;
        }

        return userService.register(new User(0, username, password, user_type, 1000.0, sex, phone));
    }
    // 3.获取个人信息
    protected RespBean getMyData(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object user = req.getSession().getAttribute("user");
        return RespBean.respSuccess(user);
    }


     // 4.编辑个人资料(username, user_type, sex, phone)
    protected RespBean updateMyData(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String user_type = req.getParameter("user_type");
        String sex = req.getParameter("sex");
        String phone = req.getParameter("phone");
        return userService.updateUser(req, username, user_type, sex, phone);
    }

    // 5.修改密码
    protected RespBean updatePassword(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String oldPassword = req.getParameter("oldPassword");
        String newPassword = req.getParameter("newPassword");
        return userService.updatePassword(req, oldPassword, newPassword);
    }
    // 6.充值
    protected RespBean addMoney(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String money = req.getParameter("money");
        String password = req.getParameter("password");
        return userService.addMoney(req, money, password);
    }
    // 7.退出登录
    protected RespBean logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().setAttribute("user", null);
        resp.sendRedirect("index.html");
        return RespBean.respSuccess("退出成功");
    }
}
